//
//  WYForwardMeController.m
//  Boss
//
//  Created by admin on 2018/8/22.
//  Copyright © 2018年 admin. All rights reserved.
//

#import "WYForwardMeController.h"
#import "WYCommentMeCell.h"
#import "WYForwardModel.h"
#import "WYRedPacketMessageController.h"
@interface WYForwardMeController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *dataArray;
@property (nonatomic,strong)NSString *messageId;
@property (nonatomic)BOOL refresh;
@end

@implementation WYForwardMeController
-(NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc]init];
    }
    return _dataArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    [self loadData];
    
    WEAKSELF
    self.tableView.mj_footer=[MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        weakSelf.refresh=NO;
        [weakSelf loadData];
        // [weakSelf loadDataWithYear:self.year];
    }];
    
    
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self clearCount];
}
-(void)clearCount
{
    [WYHttpServer cleanForwardMessageSucessBlock:^(BOOL success, id response, NSString *errorMessage) {
       
        if (success) {
            
        }
        
    }];
}

-(void)loadData
{
    WEAKSELF
    [WYHttpServer seeForwardMessageWithMessageId:self.messageId WithPageSize:@(20) SucessBlock:^(BOOL success, id response, NSString *errorMessage) {
        if (success) {
            if (weakSelf.refresh) {
                [weakSelf.dataArray removeAllObjects];
            }
            NSArray *array =[WYForwardModel getForwardArrayFromJson:response[@"result"]];
            if (array.count) {
                [weakSelf.dataArray addObjectsFromArray:array];
                WYForwardModel *model = weakSelf.dataArray.lastObject;
                self.messageId = model.messageId;
            }
            [weakSelf.tableView reloadData];
            if (array.count) {
                [weakSelf.tableView.mj_footer endRefreshing];
            }else{
                [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
            }
            
        }
        
        
    }];
}
-(void)setupUI
{
    [self.view addSubview:self.tableView];
    [self.tableView registerNib:[UINib nibWithNibName:@"WYCommentMeCell" bundle:nil] forCellReuseIdentifier:@"WYCommentMeCell"];
    self.tableView.estimatedRowHeight = 100;
    self.navigationItem.title = @"转发我的";
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WYCommentMeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WYCommentMeCell" forIndexPath:indexPath];
    if (!cell) {
        cell = [[NSBundle mainBundle]loadNibNamed:@"WYCommentMeCell" owner:nil options:nil].firstObject;
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    WYForwardModel *model = self.dataArray[indexPath.row];
    [cell setFoeward:model];
    return cell;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    WYRedPacketMessageController *rvc = [[WYRedPacketMessageController alloc]init];
    WYNearRedPacket *near = [[WYNearRedPacket alloc]init];
    WYForwardModel *model = self.dataArray[indexPath.row];
    near.redpacketId = model.redpacketId;
    near.redpacketType = @"2";
    rvc.redPacket = near;
    rvc.isCountDown = YES;
    [self.navigationController pushViewController:rvc animated:YES];
}


-(UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, self.navHeight, kScreenWidth, kScreenHeight-self.navHeight) style:(UITableViewStylePlain)];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView setSeparatorStyle:(UITableViewCellSeparatorStyleNone)];
        _tableView.backgroundColor = [UIColor hexStringToColor:@"#f7f7f7" alpha:1.0];
    }
    return _tableView;
}


@end
